package bma.common.langutil.log.filelog;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CommonFileLogTask implements FileLogTask {

	private String logId = "unknow";
	private String separator = "`";
	private StringBuffer message;

	public CommonFileLogTask() {
		super();
	}

	public CommonFileLogTask(String logId) {
		super();
		this.logId = logId;
	}

	public CommonFileLogTask(String logId, StringBuffer message) {
		super();
		this.logId = logId;
		this.message = message;
	}

	public String getLogId() {
		return logId;
	}

	public void setLogId(String logId) {
		this.logId = logId;
	}

	public StringBuffer getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = new StringBuffer(message);
	}

	public CommonFileLogTask addMessage(String msg) {
		if (this.message == null)
			this.message = new StringBuffer(128);
		if (this.message.length() > 0)
			this.message.append(this.separator);
		this.message.append(msg);
		return this;
	}

	public CommonFileLogTask addMessage(String name, String value) {
		if (this.message == null)
			this.message = new StringBuffer(128);
		if (this.message.length() > 0)
			this.message.append(this.separator);
		this.message.append(name).append('=').append(value);
		return this;
	}

	public CommonFileLogTask addNow() {
		return addNow("yyyy-MM-dd HH:mm:ss");
	}

	public CommonFileLogTask addNow(String format) {
		return addMessage("t", new SimpleDateFormat(format).format(new Date()));
	}

	@Override
	public void run(LogWriterRealm realm) {
		message.append("\n");
		realm.write(logId, message.toString());
	}

}
